package org.acooly.secretbox.ui;

import org.acooly.android.utils.AndroidUtil;
import org.acooly.secretbox.R;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

public class MainActivity extends TabActivity implements OnCheckedChangeListener {
	private RadioGroup mainTab;
	private TabHost tabHost;

	private Intent accountIntent;
	private Intent accountTypeIntent;
	private Intent impexpIntent;
	private Intent setttingIntent;

	private final static String TAB_TAG_ACCOUNT = "tab_tag_account";
	private final static String TAB_TAG_ACCOUNTTYPE = "tab_tag_accountType";
	private final static String TAB_TAG_IMPEXP = "tab_tag_impexp";
	private final static String TAB_TAG_SETTING = "tab_tag_setting";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mainTab = (RadioGroup) findViewById(R.id.main_tab);
		mainTab.setOnCheckedChangeListener(this);

		accountIntent = new Intent(this, IndexActivity.class);
		accountTypeIntent = new Intent(this, AccountTypeActivity.class);
		impexpIntent = new Intent(this, ImpexpActivity.class);
		setttingIntent = new Intent(this, SettingActivity.class);
		tabHost = getTabHost();
		tabHost.addTab(buildTabSpec(TAB_TAG_ACCOUNT, R.string.tab_account_label, R.drawable.icon_main, accountIntent));
		tabHost.addTab(buildTabSpec(TAB_TAG_ACCOUNTTYPE, R.string.tab_accountType_label, R.drawable.icon_search, accountTypeIntent));
		tabHost.addTab(buildTabSpec(TAB_TAG_IMPEXP, R.string.tab_impexp_label, R.drawable.icon_about, impexpIntent));
		tabHost.addTab(buildTabSpec(TAB_TAG_SETTING, R.string.tab_setting_label, R.drawable.icon_setting, setttingIntent));
	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) {
		return this.tabHost.newTabSpec(tag).setIndicator(getString(resLabel), getResources().getDrawable(resIcon)).setContent(content);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.tab_account:
			this.tabHost.setCurrentTabByTag(TAB_TAG_ACCOUNT);
			break;
		case R.id.tab_accountType:
			this.tabHost.setCurrentTabByTag(TAB_TAG_ACCOUNTTYPE);
			break;
		case R.id.tab_setting:
			this.tabHost.setCurrentTabByTag(TAB_TAG_SETTING);
			break;
		case R.id.tab_impexp:
			this.tabHost.setCurrentTabByTag(TAB_TAG_IMPEXP);
			break;
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
				askExiting();
				return true;
			}
		}
		return super.dispatchKeyEvent(event);
	}

	/**
	 * 询问是否退出
	 */
	private void askExiting() {
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setMessage("确定要退出吗?");
		builder.setTitle("提示");
		builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				AndroidUtil.exitApp(MainActivity.this);
			}
		});

		builder.setNeutralButton("最小化", new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				AndroidUtil.minimize(MainActivity.this);
			}
		});

		builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

}
